package com.wuhunyu.controller;


import com.wuhunyu.commonutils.result.CommonResult;
import com.wuhunyu.service.PayLogService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.Map;

/**
 * <p>
 * 支付日志表 前端控制器
 * </p>
 *
 * @author wuhunyu
 * @since 2021-01-26
 */
@RestController
@RequestMapping("/payLog")
//@CrossOrigin
public class PayLogController {

    @Autowired
    private PayLogService payLogService;

    @GetMapping("/generateCode/{orderNo}")
    public CommonResult generateCode(@PathVariable("orderNo") String orderNo) {
        return payLogService.generateCode(orderNo);
    }

    @PostMapping("/checkPayStatus/{orderNo}")
    public CommonResult checkPayStatus(@PathVariable("orderNo") String orderNo) {
        Map<String, String> map = payLogService.queryPayStatus(orderNo);
        if (map == null) {
            // 订单支付失败
            return CommonResult.error("订单支付失败");
        } else if (map.get("status") != null && "1".equals(map.get("status"))) {
            // 已支付
            Map<String, Object> resultMap = new HashMap<>(16);
            resultMap.put("status", 3);
            return CommonResult.success("订单已支付", resultMap);
        } else if ("SUCCESS".equals(map.get("trade_state"))) {
            // 订单支付成功
            payLogService.updatePayStatus(map);
            Map<String, Object> resultMap = new HashMap<>(16);
            resultMap.put("status", 2);
            return CommonResult.success("订单支付成功", resultMap);
        }
        Map<String, Object> resultMap = new HashMap<>(16);
        resultMap.put("status", 1);
        return CommonResult.success("订单支付中", resultMap);
    }

}

